package com.example.fragment_1;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.fragment_1.databinding.FragmentContentBinding;

public class ContentFragment extends Fragment {

    private static final String ARG_PARAM1 ="param" ;
    private String mParam;
    public static ContentFragment newInstance(String param) {
        ContentFragment fragment = new ContentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1,param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() !=null){
            mParam = getArguments().getString(ARG_PARAM1);
        }
    }
    private FragmentContentBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentContentBinding.inflate(inflater,container,false);
        View view = binding.getRoot();
        binding.tvContent.setText(mParam);
        binding.tvContent.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                ContentFragment.this.listener.OnItemSelected("fragment->Activity传递数据");
                Bundle bundle = new Bundle();
                bundle.putString("title_name","内容标题");
                getParentFragmentManager().setFragmentResult("title",bundle);

            }
    });
        binding.tvContent.setOnLongClickListener(v ->{
            final DeleteDialog deleteDialog = new DeleteDialog();
            deleteDialog.show(getParentFragmentManager(), "删除");
            return  true;
        });

        return view;
    }



    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.listener = (OnItemSelectedListener) context;
//        this.listener = (OnItemSelectedListener) requireActivity();
    }

    private OnItemSelectedListener listener;
    //fragment->activity传递数据的回调接口
    public interface OnItemSelectedListener{

        void OnItemSelected(String content);
    }

}
